<template>
  <el-dialog
    title="将场景映射到品牌"
    :close-on-click-modal="false"
    :visible.sync="visible"
    width="30%"
  >
    <el-transfer
      v-model="value"
      :data="data"
      :titles="['未关联', '已关联']"
      :button-texts="['取消', '关联']"
    ></el-transfer>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确认</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        sceneId: 0,
        sceneName: "",
        descript: "",
        enable: "",
        showStatus: "",
        sort: "",
        createTime: "",
      },
      data: [],
      value: [],
    };
  },
  methods: {
    init(id) {
      this.dataForm.sceneId = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        if (this.dataForm.sceneId) {
          this.$http({
            url: this.$http.adornUrl(
              `/product/scene/get/brand/${this.$cookie.get("userid")}/${
                this.dataForm.sceneId
              }`
            ),
            method: "get",
            params: this.$http.adornParams(),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.data = data.left;
              this.value = data.right
            }
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
          this.$http({
            url: this.$http.adornUrl(
            `/product/scene/set/brand/${this.$cookie.get("userid")}/${
                this.dataForm.sceneId
              }`
            ),
            method: "post",
            data: {value: this.value},
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.init(this.dataForm.sceneId);
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });

    },
  }
};
</script>
